package system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import system.httpModel.User;
import system.service.UserService;

import common.controller.BaseController;
import common.httpModel.EasyuiDataGrid;
import common.httpModel.EasyuiDataGridJson;
import common.httpModel.Json;
import common.httpModel.SessionInfo;
import common.util.IpUtil;
import common.util.ResourceUtil;


@Controller		  
@RequestMapping("/userController")
public class UserController extends BaseController {
	
	private UserService userService;
	
	
	public UserService getUserService() {
		return userService;
	}
	@Autowired
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * 用户登录
	 * @param session
	 * @param request
	 * @return
	 */
	@RequestMapping(params ="login")
	public String login(HttpSession session,HttpServletRequest request){
		SessionInfo sessionInfo=(SessionInfo) session.getAttribute(ResourceUtil.getSessionInfoName());
		User u=sessionInfo.getUser();
		
		return "index";
	}
	
	/**
	 * 用户注销
	 * @param session
	 * @return
	 */
	@RequestMapping(params="logout")
	@ResponseBody
	public Json logout(HttpSession session){
		Json j=new Json();
		if(session!=null){
			session.invalidate();
		}
		j.setSuccess(true);
		
		return j;
	}
	
	@RequestMapping(params ="checkuser")
	@ResponseBody
	public Json checkuser(User user,HttpSession session,HttpServletRequest request){
		Json j=new Json();
		User u=userService.login(user);
		if(u!=null){
			j.setSuccess(true);
			j.setMsg("登录成功！");
			
			u.setIp(IpUtil.getIpAddr(request));
			SessionInfo sessionInfo=new SessionInfo();
			sessionInfo.setUser(u);
			session.setAttribute(ResourceUtil.getSessionInfoName(), sessionInfo);
			
			j.setObj(u);
		}else{
			j.setMsg("用户名密码错误！！");
			j.setSuccess(false);
		}
		
		return j;
	}
	
	/**
	 * 显示所有用户 信息  是表格形式
	 * @param dg
	 * @param user
	 * @return
	 */
	@RequestMapping(params="datagrid")
	@ResponseBody
	public EasyuiDataGridJson datagrid(EasyuiDataGrid dg, User user){
		
		return userService.datagrid(dg,user);
	}
	
	/**
	 * 删除用户
	 * @param ids
	 * @return
	 */
	@RequestMapping(params="del")
	@ResponseBody
	public Json del(String ids){
		Json j=new Json();
		userService.del(ids);
		j.setSuccess(true);
		
		return j;
	}
	
	/**
	 * 添加用户
	 */
	@RequestMapping(params="add")
	@ResponseBody
	public User add(User user){
		
		return  userService.add(user);
	}
	
	/**
	 * 修改用户
	 */
	@RequestMapping(params="edit")
	@ResponseBody
	public Json edit(User user){
		Json j=new Json();
		try{
			userService.edit(user);
			j.setMsg("修改成功！");
			j.setSuccess(true);
		}catch (Exception e) {
			j.setSuccess(false);
			j.setMsg("修改失败，用户名存在！");
		}
		
		return  j;
	}
	
	/**
	 * 响应 上北 下南 左西 右中  首页
	 * @return
	 */
	@RequestMapping(params="north")
	public String north(){
		return "/common/layout/north";
	}
	
	@RequestMapping(params="west")
	public String west(){
		return "/common/layout/west";
	}
	
	@RequestMapping(params="center")
	public String center(){
		return "/common/layout/center";
	}
	
	@RequestMapping(params="south")
	public String south(){
		return "/common/layout/south";
	}
	
	@RequestMapping(params="home")
	public String home(){
		return "/common/layout/home";
	}
	
	/**
	 * 转换到user 控制页面
	 * @return
	 */
	@RequestMapping(params="user")
	public String user(){
		return "/admin/user";
	}
	
	/**
	 * 跳转到用户页面信息
	 * @return
	 */
	@RequestMapping(params="userInfo")
	public String userinfo(){
		return "/common/user/userInfo";
	}
	
	/**
	 * 跳转到系统探针页面
	 * 
	 * @return
	 */
	@RequestMapping(params = "jspinfo")
	public String jspinfo() {
		return "/admin/tz/jspinfo";
	}
	
	/**
	 * 编辑用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(params="editUserInfo")
	@ResponseBody
	public Json editUserInfo(User user){
		Json j=new Json();
		User u=userService.editUserInfo(user);
		if(u!=null){
			j.setSuccess(true);
		}
		return j;
	}
	
	/**
	 * 获得用户信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "getUserInfo")
	@ResponseBody
	public Json getUserInfo(HttpSession session) {
		Json j = new Json();
		SessionInfo sessionInfo = (SessionInfo) session.getAttribute(ResourceUtil.getSessionInfoName());
		if (sessionInfo != null && sessionInfo.getUser() != null) {
			User u = userService.getUserInfo(sessionInfo.getUser());
			j.setObj(u);
			j.setSuccess(true);
		} else {
			j.setMsg("您没有登录或登录超时，请重新登录后重试！");
		}
		return j;
	}
	
	/**
	 * 批量编辑用户角色
	 * 
	 * @param userIds
	 *            用户ID
	 * @param roleId
	 *            角色ID
	 * @return
	 */
	@RequestMapping(params = "editUsersRole")
	@ResponseBody
	public Json editUsersRole(String userIds, String roleId) {
		Json j = new Json();
		userService.editUsersRole(userIds, roleId);
		j.setSuccess(true);
		return j;
	}
}

